﻿Class MainWindow 
    Dim Panels As New List(Of Panel)
    Private Declare Sub GetWindowRect Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lprect As Rect)
    Private Declare Auto Function FindWindow Lib "user32.dll" (ByVal ClassName As String, ByVal WindowText As String) As IntPtr

    Structure Rect
        Dim l As Integer
        Dim t As Integer
        Dim r As Integer
        Dim b As Integer
    End Structure

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Dim TaskbarRect As Rect

        GetWindowRect(FindWindow("Shell_TrayWnd", Nothing), TaskbarRect)
        Me.Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - (TaskbarRect.b - TaskbarRect.t)
        Me.Width = My.Computer.Screen.Bounds.Width
        Dim tb As New Taskbar
        DockPanelMain.Children.Add(tb)
        DockPanel.SetDock(tb, Dock.Bottom)

        LoadPanels()
    End Sub

    Public Sub DeleteItem(ByVal item As Panel)
        If Not Panels.Count = 1 Then
            If MsgBox("Möchten Sie dieses Panel wirklich löschen?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                DockPanelMain.Children.Remove(item)
                Panels.Remove(item)
            End If
        Else
            MsgBox("Sie können das letzte Panel nicht löschen.", MsgBoxStyle.Exclamation)
        End If
    End Sub

    Public Function AddPanel(ByVal Height As Integer, ByVal Dock As Dock) As Panel
        Dim pnl As New Panel
        pnl.IsLeftPanel = Dock = Controls.Dock.Left Or Dock = Controls.Dock.Right
        pnl.ParentWindow = Me
        If pnl.IsLeftPanel Then
            pnl.Width = Height
        Else
            pnl.Height = Height
        End If
        If pnl.IsLeftPanel Then
            pnl.Background = New LinearGradientBrush(Color.FromArgb(255, 40, 40, 40), Colors.Black, 0)
        Else
            pnl.Background = New LinearGradientBrush(Color.FromArgb(255, 40, 40, 40), Colors.Black, 90)
        End If
        DockPanelMain.Children.Add(pnl)
        DockPanel.SetDock(pnl, Dock)
        Panels.Add(pnl)
        Return pnl
    End Function

    Private Sub Window_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Dim panelStr As String = ""

        For Each p As Panel In Panels
            panelStr &= "Panel " & DockPanel.GetDock(p) & " " & IIf(p.IsLeftPanel, p.Width, p.Height) & vbCr
            For Each s As Starter In p.StarterList
                panelStr &= "Starter " & """" & s.FileName & """ " & IIf(p.IsLeftPanel, Canvas.GetTop(s), Canvas.GetLeft(s)) & vbCr
            Next
            panelStr &= "EndPanel" & vbCr
        Next
        panelStr = Mid(panelStr, 1, panelStr.Length - 1)
        My.Settings.Panels = panelStr
        My.Settings.Save()
        End
    End Sub

    Private Sub LoadPanels()
        Dim PanelsSetting As String = My.Settings.Panels
        Dim IsPanel As Boolean = False
        Dim pnl As Panel

        For Each Line As String In PanelsSetting.Split(vbCr)
            Try
                If Not Line = "" Then
                    If Line.StartsWith("Panel") Then
                        IsPanel = True
                        Dim PanelProperties() As String = Line.Split(" ")
                        pnl = AddPanel(PanelProperties(2), PanelProperties(1))
                    End If
                    If Line.StartsWith("EndPanel") Then
                        IsPanel = False
                    End If
                    If Line.StartsWith("Starter") Then
                        Dim FileName As String = ""
                        Dim IsFileName As Boolean = False
                        Dim X As String = ""
                        Dim Index As Integer = 0
                        For Each c As Char In Line
                            If IsFileName Then
                                If Not c = """" Then
                                    FileName &= c
                                End If
                            End If
                            If c = """" Then
                                IsFileName = Not IsFileName
                            End If
                            If c = " " Then
                                If Not IsFileName Then
                                    Index += 1
                                End If
                            End If
                            Select Case Index
                                Case 2
                                    X &= c
                            End Select
                        Next
                        pnl.AddStarterFinish(FileName, X)
                    End If
                End If
            Catch ex As Exception

            End Try
        Next
    End Sub
End Class
